//
// Created by Henry on 16/5/3.
//
#include "com_seex_motor_MotorController.h"
#include <android/log.h>

#define LOG_TAG "MotorController"
#define LOG(...) __android_log_print(ANDROID_LOG_DEBUG,LOG_TAG, __VA_ARGS__)
#define DEVICE_NAME "/dev/motor_ctl"

#include <termios.h>
#include <unistd.h>
#include <fcntl.h>

#define MOTOR_START           0X1A
#define MOTOR_STOP            0X1B
#define MOTOR_FRONT_IO        0X1C
#define MOTOR_BACK_IO         0X1D
#define MOTOR_FOREWARD        0X1E
#define MOTOR_REVERSAL        0X1F
#define MOTOR_FOREWARD_STEP   0X2A
#define MOTOR_REVERSAL_STEP   0X2B
#define MOTOR_NET_START       0X2C
#define MOTOR_NET_STOP        0X2D
#define MOTOR_PHY_KEY_VALID   0X2F
#define MOTOR_PHY_KEY_UNVALID 0X2E
#define MOTOR_SLEEP   4000

/*
 * Class:     com_seex_motor_MotorController
 * Method:    openMotor
 * Signature: ()I
 */
JNIEXPORT jint JNICALL Java_com_seex_motor_MotorController_openMotor
        (JNIEnv *env, jobject jobj) {
    int fd = -1;
    fd = open(DEVICE_NAME, O_RDWR);

    if (fd < 0) {
        //   LOG("don't open dev");
        return -1;
    } else {
        // LOG("open success");
    }

    //LOG("open fd%d", fd);

    return fd;

}

/*
 * Class:     com_seex_motor_MotorController
 * Method:    closeMotor
 * Signature: ()I
 */
JNIEXPORT jint JNICALL Java_com_seex_motor_MotorController_closeMotor
        (JNIEnv *env, jobject jobj, jint fd) {
    LOG("close fd=%d", fd);
    int ret = close(fd);
    return ret;
}


/*
 * Class:     com_seex_motor_MotorController
 * Method:    startMotor
 * Signature: ()I
 */
JNIEXPORT jint JNICALL Java_com_seex_motor_MotorController_startMotor
        (JNIEnv *env, jobject jobj, jint fd) {
    int data = 1;
    LOG("ioctl 开始执行MOTOR_START...fd%d", fd);
    int ret = ioctl(fd, MOTOR_START, &data);
    LOG("ioctl 结束执行...ret%d", ret);

    return ret;

}

/*
 * Class:     com_seex_motor_MotorController
 * Method:    stopMotor
 * Signature: ()I
 */
JNIEXPORT jint JNICALL Java_com_seex_motor_MotorController_stopMotor
        (JNIEnv *env, jobject jobj, jint fd) {

    int data = 1;
    LOG("ioctl 开始执行MOTOR_STOP...fd%d", fd);
    int ret = ioctl(fd, MOTOR_STOP, &data);
    LOG("ioctl 结束执行...ret%d", ret);
    return ret;

}

/*
 * Class:     com_seex_motor_MotorController
 * Method:    forwardRun
 * Signature: ()I
 */
JNIEXPORT jint JNICALL Java_com_seex_motor_MotorController_forwardRun
        (JNIEnv *env, jobject jobj, jint fd) {
    LOG("forwardRun...fd=%d", fd);

    int ret = -1;
    int i = 1;
    for (i = 1; i < 9; i++) {
        ret = ioctl(fd, MOTOR_REVERSAL_STEP, &i);
        usleep(MOTOR_SLEEP); //这个时间后续可以再修改主,根据22S要求来改
    }
    return ret;
}

/*
 * Class:     com_seex_motor_MotorController
 * Method:    reverseRun
 * Signature: ()I
 */
JNIEXPORT jint JNICALL Java_com_seex_motor_MotorController_reverseRun
        (JNIEnv *env, jobject jobj, jint fd) {

    LOG("reverseRun...fd%d", fd);

    int ret = -1;
    int i = 1;
    for (i = 1; i < 9; i++) {
        ret = ioctl(fd, MOTOR_FOREWARD_STEP, &i);
        usleep(MOTOR_SLEEP); //这个时间后续可以再修改主,根据22S要求来改
    }

    return ret;
}

/*
 * Class:     com_seex_motor_MotorController
 * Method:    getStatus
 * Signature: ()I
 */
JNIEXPORT jint JNICALL Java_com_seex_motor_MotorController_getStatus
        (JNIEnv *env, jobject jobj, jint fd) {
    LOG("getStatus fd%d", fd);


    int front;
    int back;
    int status;
    int ret = 0;

    ret = ioctl(fd, MOTOR_FRONT_IO, &front);
    ret = ioctl(fd, MOTOR_BACK_IO, &back);


    LOG("front = %d , back = %d \n", front, back);

    if (front == 0 && back == 1) {
        status = 0;
    } else if (front == 1 && back == 0) {
        status = 1;
    } else {
        status = -1;
    }

    LOG("getStatus status=%d", status);


    return status;

}

JNIEXPORT jint JNICALL Java_com_seex_motor_MotorController_netStart
        (JNIEnv *env, jobject jobj, jint fd) {
    int data = 1;
    LOG("ioctl 开始执行netStart...fd%d", fd);
    int ret = ioctl(fd, MOTOR_NET_START, &data);
    LOG("ioctl 结束执行...ret%d", ret);

    return ret;
}

JNIEXPORT jint JNICALL Java_com_seex_motor_MotorController_netStop
        (JNIEnv *env, jobject jobj, jint fd) {
    int data = 1;
    LOG("ioctl 开始执行netStop...fd%d", fd);
    int ret = ioctl(fd, MOTOR_NET_STOP, &data);
    LOG("ioctl 结束执行...ret%d", ret);

    return ret;
}

JNIEXPORT jint JNICALL Java_com_seex_motor_MotorController_enableKey
        (JNIEnv *env, jobject jobj, jint fd) {
    int data = 1;
    LOG("ioctl 开始执行enableKey...fd%d", fd);
    int ret = ioctl(fd, MOTOR_PHY_KEY_VALID, &data);
    LOG("ioctl 结束执行enableKey...ret%d", ret);

    return ret;
}

JNIEXPORT jint JNICALL Java_com_seex_motor_MotorController_disableKey
        (JNIEnv *env, jobject jobj, jint fd) {
    int data = 1;
    LOG("ioctl 开始执行disableKey...fd%d", fd);
    int ret = ioctl(fd, MOTOR_PHY_KEY_UNVALID, &data);
    LOG("ioctl 结束执行disableKey...ret%d", ret);

    return ret;
}
